#include <iostream>
#include <thread>
#include "Connection.h"
#include "CommonConnectionPool.h"

using namespace std;

int main()
{
    // Connection conn;
    // // string ip, unsigned short port, string user, string password,string dbname;
    // bool n = conn.connect("127.0.0.1",8080,"Connector","12345678","conn");
    // if(n == false) cout << "Connection fail" << endl;
    // char sql[1024] = {0};
    // sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
    // conn.update(sql);

    clock_t begin = clock();

    thread t1([]()
              {
            ConnectionPool *cp = ConnectionPool::getConnectionPool();
            for (int i = 0; i < 1000; ++i)
                {
                    char sql[1024] = {0};
                    sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
                    shared_ptr<Connection> sp = cp->getConnection();
                    sp->update(sql);
                } });
    thread t2([]() {
            ConnectionPool *cp = ConnectionPool::getConnectionPool();
            for (int i = 0; i < 1000; ++i)
                {
                    char sql[1024] = {0};
                    sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
                    shared_ptr<Connection> sp = cp->getConnection();
                    sp->update(sql);
    }});
    thread t3([]() {
            ConnectionPool *cp = ConnectionPool::getConnectionPool();
            for (int i = 0; i < 1000; ++i)
                {
                    char sql[1024] = {0};
                    sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
                    shared_ptr<Connection> sp = cp->getConnection();
                    sp->update(sql);
    }});
    thread t4([]() {
            ConnectionPool *cp = ConnectionPool::getConnectionPool();
            for (int i = 0; i < 1000; ++i)
                {
                    char sql[1024] = {0};
                    sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
                    shared_ptr<Connection> sp = cp->getConnection();
                    sp->update(sql);
    }});

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    clock_t end = clock();
    cout << (end - begin) << "ms" << endl;

#if 0
for (int i = 0; i < 1000; ++i)
    {
        /*Connection conn; // 一千个人做了一件事
        // string ip, unsigned short port, string user, string password,string dbname;
        bool n = conn.connect("127.0.0.1",8080,"Connector","12345678","conn");
        if(n == false) cout << "Connection fail" << endl;
        char sql[1024] = {0};
        sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
        conn.update(sql);*/
        shared_ptr<Connection> sp = cp->getConnection();
        char sql[1024] = {0};
        sprintf(sql,"insert into user(name,age,sex) values('%s','%d','%s')","tom",18,"male");
        sp->update(sql);
    }
#endif
    return 0;
}